package com.linkhub.oms.entity.resulttype.order;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.entity.OrderEntity;
import com.linkhub.oms.entity.OrderTrackEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;

/**
 * @Description: OrderInfoDO
 * @Author: echo
 * @Date: 2024/4/2 14:07
 * @Version: 1.0
 */
@Data
public class OrderInfoDO implements Serializable {

    @ApiModelProperty("sysOrderId")
    private String sysOrderId;

    @ApiModelProperty("订单表信息")
    private OrderEntity order;

    @ApiModelProperty("orderLocalProducts")
    private List<OrderLocalProductDO> orderLocalProducts;


    @ApiModelProperty("orderShopProducts")
    private List<OrderShopProductDO> orderShopProducts;

    @ApiModelProperty("拆分订单的子订单")
    private List<OrderInfoDO> subOrders;

    @ApiModelProperty("订单追踪信息")
    private OrderTrackEntity orderTrack;

    public static OrderInfoDO emptyOrder() {
        OrderInfoDO order = new OrderInfoDO();
        order.setOrderLocalProducts(Lists.newArrayListWithCapacity(0));
        order.setOrderShopProducts(Lists.newArrayListWithCapacity(0));
        return order;
    }

    public static OrderInfoDO createDefaultOrder(String sysOrderId) {
        OrderInfoDO dto = emptyOrder();
        dto.setSysOrderId(sysOrderId);
        return dto;

    }

    @JSONField(serialize = false, deserialize = false)
    public List<String> getLocalSkuIds() {
        List<String> skuIds = Lists.newArrayList();
        List<String> localSkuIds = QtStreamUtil.map(orderLocalProducts, OrderLocalProductDO::getSkuId);
        CollUtil.addAll(skuIds, localSkuIds);

        List<String> matchSkuIds = QtStreamUtil.map(orderShopProducts, OrderShopProductDO::getLocalSkuId);
        CollUtil.addAll(skuIds, matchSkuIds);

        return skuIds;
    }

    public int acquireOrderProductQuantity() {
        int res = 0;
        res += QtStreamUtil.sumInt(orderLocalProducts, OrderLocalProductDO::getCurrentQuantity);
        res += QtStreamUtil.sumInt(orderShopProducts, OrderShopProductDO::getCurrentQuantity);

        return res;
    }

    @JSONField(serialize = false, deserialize = false)
    public Map<String,Integer> getOrderProductQuantityMap() {
        Map<String,Integer> productQuantityMap = Maps.newHashMap();
        Map<String,Integer> localProductQuantityMap = QtStreamUtil.toMap(orderLocalProducts, OrderLocalProductDO::getSkuId, OrderLocalProductDO::getCurrentQuantity);
        productQuantityMap.putAll(localProductQuantityMap);

        Map<String,Integer> matchProductQuantityMap = QtStreamUtil.toMap(orderShopProducts, OrderShopProductDO::getLocalSkuId, OrderShopProductDO::getCurrentQuantity);
        productQuantityMap.putAll(matchProductQuantityMap);

        return productQuantityMap;
    }

}
